$(function(){
    
    //类别渲染封装   尽量封装函数不然有时候会编辑渲染获取类别不到
    function init(){
        $.ajax({
            url:BigNew.category_list,
            dataType:'json',
            success(res){
                //console.log(res);
                if(res.code ==200){
                    $('.category').html(template('temp',res))
                }
            }
        })
    }
    init()

    //实现编辑预览选中信息
    var id = window.location.href.split('=')[1];
    $.ajax({
        url:BigNew.article_search,
        data:{id:id},
        dataType:'json',
        success:function(result){
            if(result.code == 200){
                //console.log(result.data.categoryId);
                $('#inputTitle').val(result.data.title)
                $('.article_cover')[0].src = result.data.cover
                $('.category').val(result.data.categoryId)
                $('#articleDate').val(result.data.date)
                $('#articleContent').val(result.data.content)
            }
        }
    })
    
    //点击图片缓存到本地并实现预览
    $('#form').on('change','#inputCover',function(){
        let files = $('#inputCover')[0].files[0]
        let url = URL.createObjectURL(files)
        //console.log(url);
        $('.article_cover')[0].src = url
    })

    //点击修改监听事件
    $('#form').on('click','.btn-success',function(){
        let form = new FormData($('#form')[0])
        form.append('content',tinyMCE.activeEditor.getContent())
        form.append('id',id)
        form.append('state','已发布')
        $.ajax({
            type:'post',
            url:BigNew.article_edit,
            data:form,
            dataType:'json',
            processData:false,
            contentType:false,
            success:function(res){
                console.log(res);
                if(res.code == 200){
                    window.location.href = './article_list.html'
                }
            }
        })
    })

    //点击修改存为草稿监听事件
    $('#form').on('click','.btn-default',function(){
        let form = new FormData($('#form')[0])
        form.append('id',id)
        form.append('content',tinyMCE.activeEditor.getContent())
        form.append('state','草稿')
        $.ajax({
            type:'post',
            url:BigNew.article_edit,
            data:form,
            dataType:'json',
            processData:false,
            contentType:false,
            success:function(res){
                console.log(res);
                if(res.code == 200){
                    window.location.href = './article_list.html'
                }
            }
        })
    })

    //时间组件
    jeDate('#articleDate', {
        trigger: 'click',
        theme: { bgcolor: "#ff0000", pnColor: "#00DDAA" },//绿色主题
        format: "YYYY-MM-DD",
        isinitVal: true,
    }) 

    // 富文本框的初始化
    tinymce.init({
        selector: '#articleContent',
        height: '350px',
        language: 'zh_CN',
        browser_spellcheck: true,
        contextmenu: false,
        plugins: [
            "advlist autolink lists link image charmap print preview anchor",
            "searchreplace visualblocks code fullscreen",
            "insertdatetime media table contextmenu paste imagetools wordcount",
            "code"
        ],
        toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | code",
    });
})